SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



ALTER     PROCEDURE [dbo].[Cicl] 


as

--DELETE FROM [i_dealer].[dbo].WorkStatus 

DECLARE 
 	@RegN varchar(8),
        @Nom bigint

SET NOCOUNT ON

--drop table #statW1

CREATE TABLE  #statW1
  (
   RegN varchar(8),
   BitN bit
  )

INSERT INTO  #statW1 (RegN,BitN)


SELECT DISTINCT CONVERT(int,RegN) as RegN,0
FROM  [i_dealer].[dbo].Automatlog AL LEFT JOIN  [i_dealer].[dbo].Automats A on AL.Automat = A.AutomatNumber 

WHERE 

 PayEnabled=1 and DateOff IS NULL  





While Exists (SELECT * FROM  #statW1 WHERE BitN=0)

  Begin

  SELECT TOP 1 @RegN=RegN FROM #statW1 where BitN=0
       
      
     exec [dbo].stat_Watch @RegN

     update  #statW1 set BitN=1 where RegN=@RegN
  end



--SELECT * FROM #statW1

--print @@rowcount


--drop table #statW1




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

